package com.sta.staff.controller.backstage;

import com.sta.staff.bean.Result;
import com.sta.staff.pojo.Users;
import com.sta.staff.service.RegService;
import com.sta.staff.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/backstage/users")
public class RegController {
    @Autowired
    private RegService regService;
    @RequestMapping("/register")
    public ModelAndView regiter(Users users, String checkCode, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        //判断验证码是否正确
        String sessionCheckCode =(String) session.getAttribute("checkCode");
        if (!checkCode.equalsIgnoreCase(sessionCheckCode)){
            modelAndView.addObject("message","验证码错误");
            modelAndView.setViewName("/backstage/register");
            return modelAndView;
        }

        Result result = regService.register(users);
        if (!result.isFlag()){
            modelAndView.addObject("message",result.getMessage());
            modelAndView.setViewName("/backstage/register");
        }else {
            modelAndView.setViewName("/backstage/register_ok");
        }
        return modelAndView;
    }
}
